package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.lab.LabUnqualifiedGroup;
import com.ysd.lis.entity.lab.LabUnqualifiedReason;
import com.ysd.lis.mapper.lab.LabUnqualifiedGroupMapper;
import com.ysd.lis.mapper.lab.LabUnqualifiedReasonMapper;
import com.ysd.lis.request.AddAndUpdateUnqualiReason;
import com.ysd.lis.request.DeleteParam;
import com.ysd.lis.service.lab.LabUnqualifiedReasonService;
import com.ysd.util.IncrementalNumberGenerator;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 不合格原因内容表 服务实现类
 * </p>
 *
 * @author Bai
 * @since 2024-02-02
 */
@Service
public class LabUnqualifiedReasonServiceImpl extends ServiceImpl<LabUnqualifiedReasonMapper, LabUnqualifiedReason> implements LabUnqualifiedReasonService {


    @Autowired
    LabUnqualifiedReasonMapper unqualifiedReasonMapper;
    @Autowired
    LabUnqualifiedGroupMapper labUnqualifiedGroupMapper;
    @Override
    public Result getUnqualifiedReasonList(LabUnqualifiedReason labUnqualifiedReason) {
        MPJLambdaWrapper<LabUnqualifiedReason> queryWrapper = new MPJLambdaWrapper<>();
//        queryWrapper.selectAll(LabUnqualifiedGroup.class);
//        queryWrapper.selectAs("lug", LabUnqualifiedReason::getReason,LabUnqualifiedGroup::getReason);
//        queryWrapper.leftJoin(LabUnqualifiedReason.class,"lug", LabUnqualifiedReason::getId,LabUnqualifiedGroup::getUnqualifiedReasonId);
        queryWrapper.eq(LabUnqualifiedReason::getDelFlag, 0);
        SysUser sysUser = RedisUserManager.getUser();
        if(ToolsUtils.isNotEmpty(sysUser)){
            queryWrapper.eq(LabUnqualifiedReason::getOrgId, sysUser.getOrgId());
        }
        if(ToolsUtils.isNotEmpty(labUnqualifiedReason)){
            if(ToolsUtils.isNotEmpty(labUnqualifiedReason.getKeywords())){
                queryWrapper.like(LabUnqualifiedReason::getReason, labUnqualifiedReason.getKeywords());
            }
        }
        queryWrapper.eq(LabUnqualifiedReason::getUnqualifiedGroupId,labUnqualifiedReason.getUnqualifiedGroupId());
        queryWrapper.orderByAsc(LabUnqualifiedReason::getSeq);
        List<LabUnqualifiedReason> list =  unqualifiedReasonMapper.selectList(queryWrapper);
        return Result.succ(list);
    }

    @Override
    public Result add(AddAndUpdateUnqualiReason addAndUpdateUnqualiReason) {
        if(ToolsUtils.isEmpty(addAndUpdateUnqualiReason)){
            return Result.fail("入参不可为空");
        }
        List<LabUnqualifiedReason> unqualifiedReasons = addAndUpdateUnqualiReason.getUnqualifiedReasons();
        SysUser sysUser = RedisUserManager.getUser();
        for (LabUnqualifiedReason unqualifiedGroup : unqualifiedReasons) {
            unqualifiedGroup.setOrgId(sysUser.getOrgId());
            unqualifiedGroup.setCode(IncrementalNumberGenerator.generateIncrementalNumber7());
        }
        boolean save = this.saveBatch(unqualifiedReasons);
        if(save){
            return Result.fail("新增成功");
        }else {
            return Result.fail("新增失败");
        }
    }

    @Override
    public Result update(AddAndUpdateUnqualiReason addAndUpdateUnqualiReason) {
        if(ToolsUtils.isEmpty(addAndUpdateUnqualiReason)){
            return Result.fail("入参不可为空");
        }
        List<LabUnqualifiedReason> unqualifiedReasons = addAndUpdateUnqualiReason.getUnqualifiedReasons();
        boolean save = this.updateBatchById(unqualifiedReasons);
        if(save){
            return Result.fail("修改成功");
        }else {
            return Result.fail("修改失败");
        }
    }

    @Override
    public Result dalete(DeleteParam deleteParam) {
        List<String> ids =  Arrays.asList(deleteParam.getIds().split(","));
        Boolean delete = this.removeByIds(ids);
        if(delete){
            return Result.fail("删除成功");
        }else {
            return Result.fail("删除失败");
        }
    }

    @Override
    public Result getUnqualifiedList(LabUnqualifiedReason labUnqualifiedReason) {
        MPJLambdaWrapper<LabUnqualifiedGroup> queryWrapper = new MPJLambdaWrapper<>();
        SysUser  sysUser =  RedisUserManager.getUser();
        if(ToolsUtils.isNotEmpty(sysUser)){
            queryWrapper.eq(LabUnqualifiedGroup::getOrgId,sysUser.getOrgId());
        }
        queryWrapper.eq(LabUnqualifiedGroup::getUnqualifiedCode,labUnqualifiedReason.getUnqualifiedCode());
        queryWrapper.eq(LabUnqualifiedGroup::getDelFlag,0);
        queryWrapper.orderByAsc(LabUnqualifiedGroup::getSeq);
        List<LabUnqualifiedGroup> labUnqualifiedGroups =  labUnqualifiedGroupMapper.selectList(queryWrapper);
        if(ToolsUtils.isNotEmpty(labUnqualifiedGroups)){
            for (LabUnqualifiedGroup labUnqualifiedGroup : labUnqualifiedGroups) {
                MPJLambdaWrapper<LabUnqualifiedReason> queryWrapperdel = new MPJLambdaWrapper<>();
                queryWrapperdel.eq(LabUnqualifiedReason::getDelFlag,0);
                queryWrapperdel.eq(LabUnqualifiedReason::getUnqualifiedGroupId,labUnqualifiedGroup.getId());
                if(ToolsUtils.isNotEmpty(sysUser)){
                    queryWrapperdel.eq(LabUnqualifiedReason::getOrgId,sysUser.getOrgId());
                }
                List list = unqualifiedReasonMapper.selectList(queryWrapperdel);
                labUnqualifiedGroup.setLabUnqualifiedReasons(list);
            }
        }
        return Result.succ(labUnqualifiedGroups);
    }
}
